
/*
  (C) 2009 Vasiliy Chekurov
*/

#include "res/resource.h"
#include "wnd.h"

bool Wnd::m_quit = false;


Wnd::Wnd() :
  m_state(0)
{
}

Wnd::~Wnd()
{
}

bool Wnd::Open(HINSTANCE hInstance,
                 HINSTANCE hPrevInstance,
                 LPSTR     lpCmdLine,
                 int       nCmdShow)
{
	MyRegisterClass( hInstance );

	// Perform application initialization:
	if (!InitInstance( hInstance, nCmdShow ))
		return FALSE;
  
  // initialize accelerator keys
  ACCEL acc[1];
  acc[0].fVirt = FNOINVERT|FVIRTKEY;
  acc[0].key   = VK_F1;
  acc[0].cmd   = ID_HELP;
  this->hAccel = CreateAcceleratorTable( acc, 1 );
  
  HICON icon = LoadIcon( this->hInst, TEXT("ICON1") );
  if (icon)
    SetClassLong( this->hWnd, GCL_HICON, (LONG)icon );
  
  HMENU menu = LoadMenu( this->hInst, TEXT("MENU1") );
  if ( menu )
  {
    SetMenu( this->hWnd, menu );
    this->hMenu = menu;
  }

  return TRUE;
}

void Wnd::Close()
{
  // release accelerator table
  if ( hAccel )
  {
    DestroyAcceleratorTable( this->hAccel );
    hAccel = 0;
  }
  
  // destroy the window
  if ( !DestroyWindow( this->hWnd ) )
  {
    MessageBox( NULL, TEXT("Error on DestroyWindow"), TEXT("Error"), MB_OK );
  }
  this->hWnd = 0;

  // unregister the window class
  if ( !UnregisterClass(TEXT("Develop Master window class"), hInst) )
  {
    MessageBox( NULL, TEXT("Error on Unregister"), TEXT("Error"), MB_OK );
  }
}

void Wnd::SetQuit(bool quit)
{
  m_quit = quit;
}

int Wnd::Run()
{
	if ( this->m_quit )
  {
    this->m_state = STATE_CLOSE;
    return 0;
  }

  MSG msg;

	if (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, this->hAccel, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
    this->m_state = STATE_RUN;
	}
  else
    this->m_state = STATE_CLOSE;

	return (int) msg.wParam;
}

ATOM Wnd::MyRegisterClass(HINSTANCE hInstance)
{
  HICON icon;
  icon = LoadIcon(NULL, IDI_APPLICATION);

	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			  = CS_DBLCLKS; //CS_HREDRAW | CS_VREDRAW;
  wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			  = icon;
	wcex.hCursor		  = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	  = NULL;
	wcex.lpszClassName	= TEXT("Develop Master window class");
	wcex.hIconSm		    = NULL;

	return RegisterClassEx(&wcex);
}

BOOL Wnd::InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  hInst = hInstance; // Store instance handle in our global variable

  hWnd = CreateWindow(TEXT("Develop Master window class"), TEXT("Develop Master"), WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  if (!hWnd)
  {
    return FALSE;
  }

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  return TRUE;
}

LONG WINAPI Wnd::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
    
    if ( !MenuHandler( hWnd, wmId, wmEvent ) )
      return DefWindowProc(hWnd, message, wParam, lParam);

		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
  case WM_CLOSE:
    SetQuit( true );
    break;
	case WM_DESTROY:
    SetQuit( true );
    break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

bool Wnd::MenuHandler(HWND hWnd, int wmId, int wmEvent)
{
  if ( wmId == ID_FILE_NEW )
  {
    MessageBox( hWnd, TEXT("File New"), TEXT(""), MB_OK );
    return true;
  }

  else if ( wmId == ID_FILE_OPEN )
  {
    MessageBox( hWnd, TEXT("Open"), TEXT(""), MB_OK );
    return true;
  }

  else if ( wmId == ID_FILE_SAVE )
  {
    MessageBox( hWnd, TEXT("Save"), TEXT(""), MB_OK );
    return true;
  }

  else if ( wmId == ID_FILE_EXIT )
  {
    SetQuit( true );
    return true;
  }

  else if ( wmId == ID_HELP )
  {
    MessageBox( hWnd, TEXT("Help"), TEXT(""), MB_OK );
    return true;
  }

  return false;
}